# !/usr/bin/env/python3
# -*- coding: utf-8 -*-
# @Author  : 影子
# @Time    : 2024/2/1 13:45
# @Software: adb_terminal_tools
# @File    : logs.py
# @Description:
from time import strftime
from time import localtime
import logging
from logging.handlers import TimedRotatingFileHandler

from main.paths import paths


class MyLogger(object):
    """日志类"""

    @staticmethod
    def create_logger():
        """日志配置"""
        my_logger = logging.getLogger("my_logger")
        my_logger.setLevel("DEBUG")
        # 控制台处理器
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel("DEBUG")
        my_logger.addHandler(stream_handler)
        # 使用时间滚动的文件处理器
        log_file_handler = TimedRotatingFileHandler(
            filename=f'{paths("logs")}{strftime("%Y-%m-%d", localtime())}.log', when='D', backupCount=10,
            encoding='utf-8')
        log_file_handler.setLevel("DEBUG")
        my_logger.addHandler(log_file_handler)
        formatter = logging.Formatter('%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s')
        stream_handler.setFormatter(formatter)
        log_file_handler.setFormatter(formatter)
        return my_logger


logger = MyLogger().create_logger()

if __name__ == '__main__':
    logger.info("info")
    logger.error("error")
